<template>
  <div class="components-container">
    <div class="header-form">
      <div class="layui-inline">
        <label class="layui-form-label">车牌号</label>
        <div class="layui-input-inline">
          <el-input v-model="selectData.carlicLike" placeholder="请输入车牌号"></el-input>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">驾驶员</label>
        <div class="layui-input-inline">
          <el-select v-model="selectData.cardricerLike" placeholder="请选择驾驶员" style="width:100%">
            <el-option v-for="item in drivers" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">车辆类别</label>
        <div class="layui-input-inline">
          <el-select v-model="selectData.cracodnameLike" placeholder="请选择车辆类别" style="width: 200px">
            <el-option label="全部" :value="null"></el-option>
            <el-option v-for="item in carTypes" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">使用日期</label>
        <div class="layui-input-inline">
          <el-date-picker v-date v-model="selectData.carstartdateEquals" type="date" placeholder="请选择日期" style="width: 200px">
          </el-date-picker>
        </div>
      </div>
      <el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">搜索</el-button>
      <el-button @click="btnRepeat">重置</el-button>
    </div>
    <div>
      <ul class="toolList">
        <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
          <el-button type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
        </li>
      </ul>
    </div>
    <dataTable :headList="headList" :dataList="dataList" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow"></dataTable>
    <carsManageMain ref="sweetModalCars" :orderType="order" @refreshData="getData"></carsManageMain>
    <staEndUse ref="useMain" @refreshData="getData"></staEndUse>
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/common.scss";

</style>
<script>
  import dataTable from "@/components/Table/DataTable";
  import staEndUse from "./staEndUse";
  import apiBase from "@/api/apiBase.js";
  import {
    formatDate,
    timeStringToDate
  } from "@/utils/date.js";
  import {
    manage
  } from "@/mixins/managePage.js";
  import carsManageMain from "./carsManageMain";
  import {
    ORDER
  } from "@/const/orderConst.js";
  const ADD = "新增";
  const MODIFY = "修改";
  const DELETE = "删除";
  const INFO = "详情";
  export default {
    name: "carsManage",
    mixins: [manage],
    data() {
      return {
        carTypes: [],
        //查询条件
        carData: {},
        drivers: [],
        selectData: {
          carlicLike: null,
          cardricerLike: null,
          cracodnameLike: null,
          carstartdateEquals: null
        }
      };
    },
    methods: {
      //车辆查询
      getData() {
        let Data = JSON.parse(JSON.stringify(this.selectData));
        Data.carstartdateEquals = formatDate(Data.carstartdateEquals, "yyyyMMdd");
        let data = {
          PageIndex: this.pageOrder.currentPage,
          PageSize: this.pageOrder.pageSize,
          OrderBy: this.pageOrder.orderType,
          OrderByType: this.pageOrder.orderSort,
          Data: Data
        };
        apiBase.DoService(data, apiBase.getApiAdress().Car.Query, res => {
          this.dataList = res.data.content.OutData;
          this.pageOrder.totalNum = res.data.content.RecordCount;
        });
      },
      //获取司机
      getDrivers() {
        let data = {};
        apiBase.DoService(data, apiBase.getApiAdress().Staff.QueryDriver, res => {
          let driverData = res.data.content,
            legendsDrivers = driverData.map(item => ({
              label: item.STANAME,
              value: item.STAID
            }));
          this.drivers = legendsDrivers;
        });
      },
      //获取车辆类型
      getCarType() {
        let data = {};
        apiBase.DoService(data, apiBase.getApiAdress().Code.GetCarType, res => {
          let carTypeData = res.data.content,
            legendsUsersData = carTypeData.map(item => ({
              label: item.CodeName,
              value: item.CodeValue
            }));
          this.carTypes = legendsUsersData;
        });
      },
      //获取表头
      getCarColumns() {
        let data = {};
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Column.GetCarColumns,
          res => {
            this.headList = res.data.content;
          }
        );
      },
      //功能操作
      operate(order) {
        this.order = order;
        this.dataRepeat();
        switch (this.order) {
          case ADD:
            this.$refs.sweetModalCars.open(this.order);
            break;
          case MODIFY:
          case INFO:
            this.$refs.sweetModalCars.open(this.order, this.currentRow.CARID);
            break;
          case DELETE:
            this.confirm(callback => {
              this.$refs.sweetModalCars.open(this.order, this.currentRow.CARID);
            });
            break;
          case ORDER.START:
          case ORDER.END:
            this.$refs.useMain.open(this.order, this.currentRow.CARID);
            break;
          default:
        }
      },
      //查询条件重置
      btnRepeat() {
        this.selectData = {
          carlicLike: null,
          cardricerLike: null,
          cracodnameLike: null,
          carstartdateEquals: null
        };
        this.getData();
      },
      //数据重置
      dataRepeat() {
        this.carData = {};
      }
    },
    components: {
      dataTable,
      staEndUse,
      carsManageMain
    },
    mounted() {},
    created() {
      this.getData();
      this.getCarType();
      this.getDrivers();
      this.getCarColumns();
    }
  };
</script>
